﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;
using BLL;

namespace IYASAKAReport
{
    public class VehicleBase
    {
        public VehicleBase()
        {
            Pass = "○";
            Fail = "×";
            NoTest = "未检";
            NoAudit = "不判定";
            Nothing = "-";
            NothingC = "无";
            PassC = "合格";
            FailC = "不合格";
            MoreThan = "≥";
            LessThan = "≤";
            Empty = "";
            Check = "√";
            FromTo = "～";
        }

        public VehicleBase(RESULT_VEHICLE_INFO result_vehicle_info, bool bSingleTest):this()
        {
            IsGC = GetIsGC(result_vehicle_info.JCLSH);
            CYS = GetCYS(result_vehicle_info.CYS);
            ZXZSL = GetZXZSL(result_vehicle_info.ZXZLXDH);
            QDZSL = GetQDZSL(result_vehicle_info.QDZWZ);
            IsNewVehicle = GetIsNewVehicle(result_vehicle_info.CCDJRQ,result_vehicle_info.GXRQ);
            IsQCLC = GetIsQCLC(result_vehicle_info.SFSQCLC);
            IsKC = GetIsKC(result_vehicle_info.CLZLDH);
            IsQYC = GetIsQYC(result_vehicle_info.RLLBDH);
            IsCYC = GetIsCYC(result_vehicle_info.RLLBDH);
            ZZL = GetZZL(result_vehicle_info.ZZL);
            ZZS = result_vehicle_info.ZZS == string.Empty ? 0 : Convert.ToInt32(result_vehicle_info.ZZS) ;
            RLLBDH = result_vehicle_info.RLLBDH;
            IsHC = GetIsHC(result_vehicle_info.CLZLDH);
            lstJCLSH = GetAllJCLSH(result_vehicle_info.JCLSH, bSingleTest);
            JCLSH = result_vehicle_info.JCLSH;
            StationInfo = GetStationInfo();
            IsYYC = GetYYC(result_vehicle_info.SYXZDH);
            IsXXKCAndWXKC = GetXXKCAndWXKC(result_vehicle_info.CLZLDH);
            IsZYC = GetZYC(result_vehicle_info.CLZLDH);
            IsSingle = bSingleTest;
            JCXH = result_vehicle_info.JCXH == null ? 0 : (int)(result_vehicle_info.JCXH);
            bQYC = GetBQYC(result_vehicle_info.CLZLDH);
            IsMTC = GetIsMTC(result_vehicle_info.CLZLDH);
            IsZSLMTC = GetZSLMTC(result_vehicle_info.CLZLDH);
            lstJYXM = GetAllJYXM(result_vehicle_info.JYXM);
        }

        public string Pass { get; set; }
        public string Fail { get; set; }
        public string NoTest { get; set; }
        public string NoAudit { get; set; }
        public string Nothing { get; set; }
        public string NothingC { get; set; }
        public string PassC { get; set; }
        public string FailC { get; set; }
        public string MoreThan { get; set; }
        public string LessThan { get; set; }
        public string Empty { get; set; }
        public string Check { get; set; }
        public string FromTo { get; set; }

        public bool IsSingle { get; set; }

        /// <summary>
        /// 是否为挂车
        /// </summary>
        public bool IsGC { get; private set; }

        /// <summary>
        /// 是否是摩托车
        /// </summary>
        public bool IsMTC { get; private set; }

        /// <summary>
        /// 是否是正三轮摩托车
        /// </summary>
        public bool IsZSLMTC { get; set; }

        /// <summary>
        /// 乘员数
        /// </summary>
        public int CYS { get; private set; }

        /// <summary>
        /// 转向轴数量
        /// </summary>
        public int ZXZSL { get; private set; }

        /// <summary>
        /// 驱动轴数量
        /// </summary>
        public int QDZSL { get; private set; }

        /// <summary>
        /// 是否是新车
        /// </summary>
        public bool IsNewVehicle { get; private set; }

        /// <summary>
        /// 是否是汽车列车
        /// </summary>
        public bool IsQCLC { get; private set; }

        /// <summary>
        /// 是否是客车
        /// </summary>
        public bool IsKC { get; private set; }

        /// <summary>
        /// 是否是作业车
        /// </summary>
        public bool IsZYC { get; private set; }

        /// <summary>
        /// 是否为汽油车
        /// </summary>
        public bool IsQYC { get; private set; }

        /// <summary>
        /// 是否为柴油车
        /// </summary>
        public bool IsCYC { get; private set; }

        /// <summary>
        /// 是否是小型客车和微型客车
        /// </summary>
        public bool IsXXKCAndWXKC { get; private set; }

        /// <summary>
        /// 是否为营运车辆
        /// </summary>
        public bool IsYYC { get; private set; }

        /// <summary>
        /// 总质量
        /// </summary>
        public int ZZL { get; private set; }

        /// <summary>
        /// 总轴数
        /// </summary>
        public int ZZS { get; private set; }

        /// <summary>
        /// 是否为货车
        /// </summary>
        public bool IsHC { get; private set; }

        /// <summary>
        /// 是否为牵引车
        /// </summary>
        public bool bQYC { get; private set; }

        /// <summary>
        /// 得到检测流水号集合
        /// </summary>
        public List<string> lstJCLSH { get; private set; }

        /// <summary>
        /// 得到检验项目集合
        /// </summary>
        public List<string> lstJYXM { get; private set; }

        /// <summary>
        /// 当前车检测流水号
        /// </summary>
        public string JCLSH { get; private set; }

        /// <summary>
        /// 燃料类别
        /// </summary>
        public string RLLBDH { get; private set; }

        /// <summary>
        /// 报告单是否显示完成检测流水号
        /// </summary>
        public bool bShowAllJCLSH { get; set; }

        /// <summary>
        /// 检测线号
        /// </summary>
        public int JCXH { get; private set; }

        /// <summary>
        /// 检测站的基本信息
        /// </summary>
        public INSPECTION_STATION_INFO StationInfo { get; private set; }

        /// <summary>
        /// 得到所有检测流水号
        /// </summary>
        /// <param name="strJCLSH"></param>
        /// <param name="bSingle"></param>
        /// <returns></returns>
        public List<string> GetAllJCLSH(string strJCLSH, bool bSingle)
        {
            List<string> lstJCLSH = new List<string>();
            if (bSingle)
            {
                lstJCLSH.Add(strJCLSH);
            }
            else
            {
                if (strJCLSH.Contains("-"))
                {
                    string[] strInfos = strJCLSH.Split('-');
                    int iJCCS = Convert.ToInt32(strInfos[1]) + 1;
                    for (int i = 1; i < iJCCS; i++)
                    {
                        lstJCLSH.Add(strInfos[0] + "-" + i.ToString("d2"));
                    }
                }
                else
                {
                    lstJCLSH.Add(strJCLSH);
                }
            }

            return lstJCLSH;
        }

        /// <summary>
        /// 是否为挂车
        /// </summary>
        /// <param name="strJCLSH"></param>
        /// <returns></returns>
        private bool GetIsGC(string strJCLSH)
        {
            return strJCLSH.Substring(7, 1) == "G" ? true : false;
        }

        /// <summary>
        /// 是否为摩托车
        /// </summary>
        /// <param name="strHPZL"></param>
        /// <returns></returns>
        private bool GetIsMTC(string strCLZLDH)
        {
            if (strCLZLDH.Substring(0, 1) == "M")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否是正三轮摩托车
        /// </summary>
        /// <param name="CLZLDH"></param>
        /// <returns></returns>
        private bool GetZSLMTC(string strCLZLDH)
        {
            if (strCLZLDH == "M11" || strCLZLDH == "M12" || strCLZLDH == "M13" || strCLZLDH == "M14")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 得到乘员数
        /// </summary>
        /// <param name="strCYS"></param>
        /// <returns></returns>
        private int GetCYS(string strCYS)
        {
            if (string.IsNullOrEmpty(strCYS) == false)
            {
                return Convert.ToInt32(strCYS);
            }
            return 0;
        }

        /// <summary>
        /// 是否为营运车辆
        /// </summary>
        /// <param name="SYXZDH"></param>
        /// <returns></returns>
        private bool GetYYC(string strSYXZDH)
        {
            if (strSYXZDH.Substring(0, 1) == "Y")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 得到转向轴数量
        /// </summary>
        /// <param name="strZXZSLDH"></param>
        /// <returns></returns>
        private int GetZXZSL(string strZXZSLDH)
        {
            if (strZXZSLDH == "1")
            {
                return 1;
            }
            else if (strZXZSLDH == "2")
            {
                return 2;
            }
            else
            {
                return 1;
            }
        }

        /// <summary>
        /// 得到驱动轴数量
        /// </summary>
        /// <param name="strQDZWZ"></param>
        /// <returns></returns>
        private int GetQDZSL(string strQDZWZ)
        {
            int iRet = 0;
            if (string.IsNullOrEmpty(strQDZWZ) == false)
            {
                strQDZWZ = strQDZWZ.Substring(0, strQDZWZ.Length - 1);
                if (strQDZWZ.Contains(','))
                {
                    iRet = strQDZWZ.Split(',').Length;
                }
                else
                {
                    iRet = 1;
                }
            }
            return iRet;
        }

        /// <summary>
        /// 得到是否为新车
        /// </summary>
        /// <param name="strCCDJRQ">初始登记日期</param>
        /// <param name="strGXRQ">过线日期</param>
        /// <returns></returns>
        private bool GetIsNewVehicle(string strCCDJRQ,string strGXRQ)
        {
            if (!string.IsNullOrEmpty(strCCDJRQ) && !string.IsNullOrEmpty(strGXRQ))
            {
                return Convert.ToDateTime(strCCDJRQ).AddMonths(3) > Convert.ToDateTime(strGXRQ) ? true : false;
            }

            return false;
        }

        /// <summary>
        /// 是否是汽车列车
        /// </summary>
        /// <param name="strSFSQCLC"></param>
        /// <returns></returns>
        private bool GetIsQCLC(string strSFSQCLC)
        {
            if (string.IsNullOrEmpty(strSFSQCLC) == false)
            {
                return Convert.ToInt32(strSFSQCLC) == 1 ? true : false;
            }

            return false;
        }

        /// <summary>
        /// 是否为客车
        /// </summary>
        /// <param name="strCLZLDH"></param>
        /// <returns></returns>
        private bool GetIsKC(string strCLZLDH)
        {
            if (strCLZLDH.Substring(0, 1) == "K")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否为货车
        /// </summary>
        /// <param name="strCLZLDH"></param>
        /// <returns></returns>
        private bool GetIsHC(string strCLZLDH)
        {
            if (strCLZLDH.Substring(0, 1) == "H")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否为牵引车
        /// </summary>
        /// <param name="strCLZLDH"></param>
        /// <returns></returns>
        private bool GetBQYC(string strCLZLDH)
        {
            if (strCLZLDH.Substring(0, 1) == "Q")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否是柴油车
        /// </summary>
        /// <param name="strRLLBDH"></param>
        /// <returns></returns>
        private bool GetIsCYC(string strRLLBDH)
        {
            if (strRLLBDH == "B" || strRLLBDH == "D" || strRLLBDH == "F")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否是微型载客车和小型载客车
        /// </summary>
        /// <param name="strCLZLDH"></param>
        /// <returns></returns>
        private bool GetXXKCAndWXKC(string strCLZLDH)
        {
            string strType = strCLZLDH.Substring(0, 2);
            if (strType == "K3" || strType == "K4")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 是否是汽油车
        /// </summary>
        /// <param name="strRLLBDH"></param>
        /// <returns></returns>
        private bool GetIsQYC(string strRLLBDH)
        {
            if (strRLLBDH == "A" || strRLLBDH == "E" || strRLLBDH == "O" || strRLLBDH == "L" || strRLLBDH == "G")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否是作业车
        /// </summary>
        /// <param name="strCLZLDH"></param>
        /// <returns></returns>
        private bool GetZYC(string strCLZLDH)
        {
            if (strCLZLDH.Substring(0, 1) == "Z")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到总质量
        /// </summary>
        /// <param name="strZZL"></param>
        /// <returns></returns>
        private int GetZZL(string strZZL)
        {
            if (string.IsNullOrEmpty(strZZL) == false)
            {
                return Convert.ToInt32(strZZL);
            }

            return 0;
        }

        protected string GetPDResult(string str)
        {
            string strValue = "";
            if (!string.IsNullOrEmpty(str.Trim()))
            {
                switch (str.Trim())
                {
                    case "1"://合格
                        strValue = PassC;
                        break;
                    case "2"://不合格
                        strValue = FailC;
                        break;
                    case "0"://未检
                        strValue = NoTest;
                        break;
                    case "3"://不判定
                        strValue = NoAudit;
                        break;
                    case "-":
                        strValue = Nothing;
                        break;
                    default:
                        strValue = Nothing;
                        break;
                }
            }
            else
            {
                strValue = Nothing;
            }

            return strValue;
        }

        /// <summary>
        /// 得到判定结果，并返回不合格的内容
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strItemName"></param>
        /// <returns></returns>
        protected List<string> GetPDResult(string str,string strItemName)
        {
            List<string> lstRet = new List<string>();
            string strValue = "";
            string strName = "";
            if (!string.IsNullOrEmpty(str.Trim()))
            {
                switch (str.Trim())
                {
                    case "1"://合格
                        strValue = PassC;
                        break;
                    case "2"://不合格
                        strValue = FailC;
                        strName = strItemName;
                        break;
                    case "0"://未检
                        strValue = NoTest;
                        break;
                    case "3"://不判定
                        strValue = NoAudit;
                        break;
                    case "-":
                        strValue = Nothing;
                        break;
                    default:
                        strValue = Nothing;
                        break;
                }
            }
            else
            {
                strValue = Nothing;
            }

            lstRet.Add(strValue);
            lstRet.Add(strName);

            return lstRet;
        }

        /// <summary>
        /// 初始化Datable值为Nothing
        /// </summary>
        /// <param name="dt"></param>
        public DataRow SetDataTableDefaultValue(ref DataTable dt, string defaultValue)
        {
            DataRow dr = dt.NewRow();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Columns[i].ColumnName == "JCLSH")
                {
                    dr[dt.Columns[i].ColumnName] = JCLSH;
                }
                else if (dt.Columns[i].ColumnName.ToLower().Contains("image"))
                {
                    try
                    {
                        dr[dt.Columns[i].ColumnName] = null;
                    }
                    catch (Exception ex)
                    {
                        dr[dt.Columns[i].ColumnName] = "";
                    }
                }
                else
                {
                    try
                    {
                        dr[dt.Columns[i].ColumnName] = defaultValue;
                    }
                    catch (Exception ex)
                    {
                        dr[dt.Columns[i].ColumnName] = "1";
                        continue;
                    }
                }
            }

            return dr;
        }

        /// <summary>
        /// 得到检测站的基本信息
        /// </summary>
        /// <returns></returns>
        private INSPECTION_STATION_INFO GetStationInfo()
        {
            INSPECTION_STATION_INFO info = new INSPECTION_STATION_INFO();
            INSPECTION_STATION_INFO_BLL info_bll = new INSPECTION_STATION_INFO_BLL();
            var infos = info_bll.GetModelList();

            if (infos.Length > 0)
            {
                info = infos[0];
            }

            return info;

        }
        /// <summary>
        /// 获得测项目
        /// </summary>
        /// <param name="jyxm"></param>
        /// <returns></returns>
        public string AJGetJYXM(string jyxm)
        {
            List<string> lst = new List<string>();
            RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
            if (!IsSingle)
            {
                foreach (var v in lstJCLSH)
                {
                    var vInfo = bll.GetModel(v);

                    string[] strJYXMs = vInfo.JYXM.Split(',');

                    foreach (var item in strJYXMs)
                    {
                        if (string.IsNullOrEmpty(item) == false)
                        {
                            int iCount = lst.Where(x => x.ToUpper().Contains(item.Substring(0, 1))).ToList().Count;
                            if (iCount == 0 && item.Contains("Z") == false)
                            {
                                lst.Add(item.Substring(0, 1));
                            }
                        }
                    }

                }
            }
            else
            {

                if (!string.IsNullOrEmpty(jyxm))
                {
                    jyxm = jyxm.Substring(0, jyxm.Length - 1);
                }

                if (jyxm.Contains(","))
                {
                    string[] items = jyxm.Split(',');
                    foreach (var v in items)
                    {
                        int iCount = lst.Where(x => x.ToUpper().Contains(v.Substring(0, 1))).ToList().Count;
                        if (iCount == 0 && v.Contains("Z") == false)
                        {
                            lst.Add(v.Substring(0, 1));
                        }
                    }
                }
                else
                {
                    lst.Add(jyxm.Substring(0, 1));
                }
            }

            lst = lst.Where(x => x == "B" || x == "H" || x == "S" || x == "A").ToList();

            string strRet = "";
            foreach (var v in lst)
            {

                strRet += v;
            }

            return strRet;
        }

        /// <summary>
        /// 得到检测项目
        /// </summary>
        /// <returns></returns>
        protected string GetJYXM()
        {
            string strJYXM = "";
            RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
            for (int i = 0; i < lstJCLSH.Count; i++)
            {
                var vInfo = bll.GetModel(lstJCLSH[i]);
                strJYXM +=  vInfo.JYXM;
            }

            string[] lstJYXM = strJYXM.Split(',');
            List<string> lst = new List<string>();
            for (int i = 0; i < lstJYXM.Length; i++)
            {
                if (string.IsNullOrEmpty(lstJYXM[i]) == false)
                {
                    if (lst.Contains(lstJYXM[i]) == false)
                    {
                        lst.Add(lstJYXM[i]);
                    }
                }
            }

            strJYXM = "";
            for (int i = 0; i < lst.Count; i++)
            {
                strJYXM += lst[i] + ",";
            }

            return strJYXM;
        }


        /// <summary>
        /// 得到所有的检测项目
        /// </summary>
        /// <param name="strJYXM"></param>
        /// <returns></returns>
        private List<string> GetAllJYXM(string strJYXM)
        {
             List<string> lstRet = new List<string>();
            RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
            foreach (var v in lstJCLSH)
            {
                var vInfo = bll.GetModel(v);
                if (vInfo.JYXM.Contains("R1") && lstRet.Contains("R1") == false)
                {
                    lstRet.Add("R1");
                }

                if (vInfo.JYXM.Contains("R2") && lstRet.Contains("R2") == false)
                {
                    lstRet.Add("R2");
                }
            }

           
            if (string.IsNullOrEmpty(strJYXM) == false)
            {
                if (strJYXM.Contains(","))
                {
                    string[] strAry = strJYXM.Split(',');
                    foreach (var v in strAry)
                    {
                        if (string.IsNullOrEmpty(v) == false && lstRet.Contains(v) == false)
                        {
                            lstRet.Add(v);
                        }
                    }
                }
                else
                {
                    lstRet.Add(strJYXM);
                }
            }
            return lstRet;
        }

        private void AllJYXM()
        {
 
        }
    }
}
